/**
 * outsider.com.cn Inc.
 * Copyright (c) 2011-2022 All Rights Reserved.
 */
package com.yuanmeng.common.core.handler;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.yuanmeng.common.core.enums.ErrorCodeEnum;
import com.yuanmeng.common.core.exception.ServiceException;

/**
 * @author outsider
 * @version Id: FormatValidateHandler.java, v 0.1 2022/6/6 17:47 outsider Exp $$
 */
public class FormatValidateHandler {
    /**
     * 验证为手机号的正则表达式
     */
    private static final String PHONE_NUMBER = "^1[345789]\\d{9}";

    /**
     * 校验手机号是否合法
     * @param phone 手机号码
     * @return 返回true,否则为false
     */
    public static void phoneNumValidate(String phone) {
        if (phone.length() != 11) {
            throw new ServiceException(ErrorCodeEnum.PHONE_NUM_FORMAT_IS_ERROR);
        } else {
            Pattern p = Pattern.compile(PHONE_NUMBER);
            Matcher m = p.matcher(phone);
            boolean isMatch = m.matches();
            if (!isMatch) {
                throw new ServiceException(ErrorCodeEnum.PHONE_NUM_FORMAT_IS_ERROR);
            }
        }
    }
}